package com.bw.utils;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class KeywordUtil {
    //分词 将字符串进行分词，将分词之后的结果放到一个集合中返回
    public static List<String> analyze(String text) {
        List<String> wordList = new ArrayList<>();
        //将字符串转换为字符输入流
        StringReader sr = new StringReader(text);
        //创建分词器对象
        IKSegmenter ikSegmenter = new IKSegmenter(sr, true);
        // Lexeme 是分词后的一个单词对象
        Lexeme lexeme = null;
        //通过循环，获取分词后的数据
        while (true) {
            try {
                //获取一个单词
                if ((lexeme = ikSegmenter.next()) != null) {
                    String word = lexeme.getLexemeText();
                    wordList.add(word);
                } else {
                    break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return wordList;
    }

    public static void main(String[] args) {
        String word = "山东东北大快车乐乐";
        List<String> analyze = analyze(word);
        for (String s : analyze) {
            System.out.println("s = " + s);
        }
    }
}



